/**
 * 初始化Vuex入口文件
 */

import Vue from 'vue'
import Vuex from 'vuex'
import { getNaviMenu } from '@/api'
//Vuex 内置日志插件用于一般的调试
import createLogger from 'vuex/dist/logger'

Vue.use(Vuex)

//只在开发环境时启动严格模式
const debug = process.env.NODE_ENV !== 'production'

//工厂方法输出一个单例Vuex.Store模式
const store = new Vuex.Store({
    state:{
        navmenus: [],
        language: 'zh',
        languageid: 0
    },
    getters:{
        navmenus: state => state.navmenus,
        language: state => state.language,
        languageid: state => state.languageid
    },
    mutations:{
        SET_NAVMENUS: (state, menus) => {
            state.navmenus = menus
        },
        SET_LANGUAGE: (state, languageid, language) => {
            if(state.languageid === languageid || state.language === language) return;
            state.language = language;
            state.languageid = languageid;
        }
    },
    actions:{
        GetNavMenusAsync ({ commit }, langid) {
            return new Promise((resolve, reject) => {
                getNaviMenu(langid, function(res) {
                    //console.log('navmenu', res.data);
                    commit('SET_NAVMENUS', res.data);
                    resolve(res.data);
                }, function(msg) {
                    reject(new Error(msg));
                });
            });
        },
    },
    plugins: debug ? [createLogger()] : []
})
export default store